package thread.bus;

import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

public class BusMain {

    //初始化公交路线一共15站
//    public static  final CopyOnWriteArrayList<Integer> routes = new CopyOnWriteArrayList<Integer>();
    public static  final CopyOnWriteArrayList<Passenger> PASSENGERS_UPPER = new CopyOnWriteArrayList<Passenger>();
    public static  final CopyOnWriteArrayList<Passenger> PASSENGERS_LOWER = new CopyOnWriteArrayList<Passenger>();



//    static {
//        for (int i=1;i<=15;i++){
//            routes.add(i);
//        }
//    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(()->{
            for (int i = 1;i<=10;i++){
                if (i>5) {
                    Thread thread = new Thread(new Bus("公交车"+i + "", 15));
                    thread.start();
                }else {
                    Thread thread = new Thread(new Bus("公交车"+ i + "", 1));
                    thread.start();
                }
                try {
                    TimeUnit.MINUTES.sleep(15);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();



        new Thread(()->{
            // 每五分钟会有10名乘客上车，从哪个站到哪个站
            while (true){
                for (int i = 0;i<10;i++){
                    Passenger passenger = new Passenger();
                    int source = new Random().nextInt(15)+1;
                    int targer =  source+new Random().nextInt(4)+1;
                    targer = targer>15?15:targer;
                    if (source==targer){
                        if (source==15){
                            source = 1;
                            targer = 15;
                        }
                    }
                    passenger.setSource(source);
                    passenger.setTarger(targer);
                    //上行的乘客
                    PASSENGERS_UPPER.add(passenger);

                    Passenger passenger1 = new Passenger();
                    int source1 = new Random().nextInt(15)+1;
                    int targer1 =  source-new Random().nextInt(4)+1;
                    targer1 = targer1<1?1:targer1;
                    if (targer1==source1){
                        if (source1==1){
                            source1 = 15;
                            targer1 = 1;
                        }
                    }
                    passenger1.setSource(source1);
                    passenger1.setTarger(targer1);
                    //下行的10名乘客
                    PASSENGERS_LOWER.add(passenger1);

                }
                //每五分钟一次
                try {
                    TimeUnit.MINUTES.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        Thread.sleep(20000);
    }

}
